package com.rf.richfitwheel.admin.sys.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.rf.richfitwheel.admin.sys.vo.UserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.rf.richfitwheel.admin.annotation.ModifyAnnotaionFactory;
import com.rf.richfitwheel.admin.annotation.UserOperateLog;
import com.rf.richfitwheel.admin.contants.UrlConstant;
import com.rf.richfitwheel.admin.sys.model.Tenant;
import com.rf.richfitwheel.admin.sys.model.User;
import com.rf.richfitwheel.admin.sys.service.TenantService;
import com.rf.richfitwheel.admin.utils.UserUtil;
import com.rf.richfitwheel.common.contants.CommonServiceError;
import com.rf.richfitwheel.common.exception.BusinessException;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.R;

/**
 * 租户管理
 * @author hushouquan
 *
 */
@RestController
@RequestMapping(UrlConstant.WEB_ADMIN_URL+"/tenant")
public class SysTenantController {
	
	private static Logger log = LoggerFactory.getLogger(SysTenantController.class);
	
	@Autowired
	private TenantService tenantService;
	
	/**
	 * 分页查询
	 */
	@GetMapping("/list")
	public R list(String page, String limit, String dataForm){
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("page", page);
			params.put("limit", limit);
			params.put("dataForm", dataForm);
			PageUtils pages = tenantService.queryPage(params);
			return R.ok().put("page", pages);
		} catch (Exception e) {
			log.error("查询数据失败", e);
			return R.error("查询数据失败");
		}
	}
	
	/**
	 * 根据id查询信息
	 * @param id
	 * @return
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) {
		try {
			Tenant tenant = tenantService.selectById(id);
			return R.ok().put("tenant", tenant);
		} catch (Exception e) {
			log.error("查询数据失败", e);
			return R.error("查询数据失败");
		}
	}
	
	/**
	 * 新增租户
	 * @param tenant
	 * @return
	 */
	@UserOperateLog(module="系统功能",methods="save",description="新增租户信息",functionNo="101056")
	@PostMapping("/save")
	public R save(@RequestBody Tenant tenant) {
		try {
			UserVO userVO = UserUtil.getUser();
			tenantService.save(tenant, userVO);
			//给日志注解传入业务id		
	        ModifyAnnotaionFactory.newInstance().ModifyAnnotation(getClass(), UserOperateLog.class, "save", "bussid", tenant.toString(), tenant.getClass());
			return R.ok();
		} catch(BusinessException e) {
			return R.error(e.getMessage());
		}catch (Exception e) {
			log.error(CommonServiceError.SERVICE_SAVE + "租户信息" + CommonServiceError.SERVICE_FAILURE.getMsg(), e);
			return R.error(CommonServiceError.SERVICE_SAVE + "租户信息" + CommonServiceError.SERVICE_FAILURE.getMsg());
		}
	}
	
	/**
	 * 更新租户信息
	 * @param tenant
	 * @return
	 */
	@UserOperateLog(module="系统功能",methods="update",description="更新租户信息",functionNo="101057")
	@PostMapping("/update")
	public R update(@RequestBody Tenant tenant) {
		try {
			UserVO userVO = UserUtil.getUser();
			tenantService.update(tenant, userVO);
			//给日志注解传入业务id		
	        ModifyAnnotaionFactory.newInstance().ModifyAnnotation(getClass(), UserOperateLog.class, "update", "bussid", tenant.toString(), tenant.getClass());
			return R.ok();
		} catch(BusinessException e) {
			return R.error(e.getMessage());
		}catch (Exception e) {
			log.error(CommonServiceError.SERVICE_SAVE + "租户信息" + CommonServiceError.SERVICE_FAILURE.getMsg(), e);
			return R.error(CommonServiceError.SERVICE_SAVE + "租户信息" + CommonServiceError.SERVICE_FAILURE.getMsg());
		}
	}
	
	/**
	 * 删除租户信息
	 * @param roleIds 
	 * @return
	 */
	@PostMapping("/delete")
//	@RequiresPermissions("sys:role:delete")
	@UserOperateLog(module="系统功能",methods="delete",description="删除租户信息",functionNo="101058")
	public R delete(@RequestBody String[] roleIds){
		try {
			for (String roleId: roleIds) {
				if("1".equals(roleId)){
					return R.error("内置租户请勿删除！");
				}
			}
			tenantService.deleteBatch(roleIds);
			ModifyAnnotaionFactory.newInstance().ModifyAnnotation(getClass(), UserOperateLog.class, "delete", "bussid", roleIds.toString(), roleIds.getClass());
			return R.ok();
		} catch (Exception e) {
			log.error(CommonServiceError.SERVICE_MODIFY.getMsg() + "租户信息" + CommonServiceError.SERVICE_FAILURE.getMsg(), e);
			return R.error(CommonServiceError.SERVICE_MODIFY.getMsg() + "租户信息" + CommonServiceError.SERVICE_FAILURE.getMsg());
		}
	}
	
	@GetMapping("/getTenantSelect")
	public R getTenantSelect() {
		List<Map<String, Object>> list = tenantService.getTenantSelect();
		return R.ok().put("list", list);
	}
}
